package res

import (
	"encoding/json"
	"github.com/kataras/iris"
	"github.com/kataras/iris/mvc"
)

func New() *ApiJson {
	return new(ApiJson)
}

type ApiJson struct {
}

func (j *ApiJson) Success(message string, data interface{}) mvc.Response {
	txt := make(map[string]interface{})
	txt["message"] = message
	txt["code"] = iris.StatusOK
	txt["state"] = 1
	txt["data"] = data
	bytes, _ := json.Marshal(&txt)
	return mvc.Response{
		Code:        iris.StatusOK,
		ContentType: "application/json",
		Text:        string(bytes),
	}
}

func (j *ApiJson) Fail(message string,code int) mvc.Response {
	txt := make(map[string]interface{})
	txt["message"] = message
	txt["code"] = code
	txt["state"] = 0
	bytes, _ := json.Marshal(&txt)
	return mvc.Response{
		Code:        iris.StatusOK,
		ContentType: "application/json",
		Text:        string(bytes),
	}
}

func (j *ApiJson) Error(message string,code int) mvc.Response {
	txt := make(map[string]interface{})
	txt["message"] = message
	txt["state"] = 0
	bytes, _ := json.Marshal(&txt)

	return mvc.Response{
		Code:        code,
		ContentType: "application/json",
		Text:        string(bytes),
	}
}
